
package XML作业;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLXiugai {
	public static void main(String[] args) throws DocumentException, IOException {
		Scanner in = new Scanner(System.in);
		SAXReader reader = new SAXReader();
		Document doc = reader.read("products.xml");
		Element root = doc.getRootElement();
		System.out.println("请输入商品编号");
		String num = "product[itemno=" + in.next() + "]";
		List<Element> list = root.selectNodes(num);
		System.out.println(list.size());
		if (list.size() == 0) {
			System.out.println("该商品不存在");
			return;
		}
		System.out.println("请输入您要修改的内容");
		int xiugai = in.nextInt();
		for (Element e : list) {
			e.selectSingleNode("stockcount").setText(xiugai + "");
		}
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter xm = new XMLWriter(new FileWriter("products.xml"), format);
		xm.write(doc);
		xm.close();
	}
}
